#!/bin/bash
#pMusic - export tracks
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
cd $HOME
echo ' ' > $WORKDIR/splash_copy_text #echo nothing will delete existing splash
. $APPDIR/gui_mixer

#read config
[ -f $WORKDIR/rcexport ] && . $WORKDIR/rcexport
#check if rc really exist
if [ ! "$COPY_BITRATE" ]; then
	COPYDIR=$HOME
	SPLIT_RADIOSTREAM=true
	ADD_NUMBERING=false
	FIRST_NR=1
	CHK_OUTPUT_STREAM=false
	COPY_BITRATE=192000
	COPY_FORMAT=mp3
	COPY_SAMPLE=44100
	COPY_CHANNEL=2
	CD_FILE_NAME='%a - %t'
fi
#exporting web-stream with $lENGTH determined without streamripper, we need to convert output. Radio has length 00:00
[ "$(grep -E "*.kar$|*.mid$|*http*" | grep -vF '00:00')" ] && CHK_OUTPUT_STREAM=true
#---
echo "$COPY_FORMAT"		> $WORKDIR/export_copy_format
echo "$COPY_BITRATE"	> $WORKDIR/export_copy_bitrate
echo "$COPY_SAMPLE"		> $WORKDIR/export_copy_sample
echo "$COPY_CHANNEL"	> $WORKDIR/export_copy_channel
[ ! -s $WORKDIR/export_presets ] &&	echo "$(gettext 'Music medium quality')|mp3,192000,44100,2
$(gettext 'Music loseless quality')|flac,,,
$(gettext 'Audio-book compressed')|mp3,32000,22050,1
$(gettext 'CD-audio')|wav,1411000,44100,2" > $WORKDIR/export_presets

cp -f $HOME/.pmusic/nowplaying_albumart.jpg $WORKDIR/tmp_export_albumart.jpg


info (){
	HEADING="$(gettext 'Export')"
	FRAME="$(gettext 'Info')"
	IMG=info
	TXT1="$(gettext 'The Export operation will save all supported files/streams in the list. If chosen, the output file can be converted to another format/quality.

Middle-click on file to remove it from the list.

<b>Grab radio-stream</b>
If the stream contains meta-data (most do), each song (file) is named with the recieved Artist - Title information.
A more comprehensive grabber is found in the Tools menu.

<b>Rip CD-audio</b>
pMusic will try to grab CD-disc information from the internet. You can decide which part of the info to use for naming of the copied tracks.')
	<b>%a</b> - $(gettext 'Artist')
	<b>%A</b> - $(gettext 'Album')
	<b>%n</b> - $(gettext 'Track-number')
	<b>%t</b> - $(gettext 'Title')"
	. $APPDIR/box_ok &
}

fetch_dir (){
	TITLE="- $(gettext 'Add file(s) to list')"
	XML_CHOOSER='
	<hseparator></hseparator>
	<checkbox>
	 <variable>CHK_RECURSIVE</variable>
	 <label>"'$(gettext 'Add recursively (also add files from sub-directories)')'"</label>
	</checkbox>'
	. $APPDIR/box_chooser
	case $(file "$CHOOSER") in
		*directory*)
			if [ $CHK_RECURSIVE = true ]; then
				find "$CHOOSER" | grep -E "$AUDIO_FORMATS" >> $WORKDIR/exportlist
			else
				find "$CHOOSER" -mindepth 1 -maxdepth 1 -printf "%p\n" | grep -E "$AUDIO_FORMATS" >> $WORKDIR/exportlist
			fi
			;;
		*)
			echo "$CHOOSER" >> $WORKDIR/exportlist
			;;
	esac
}

exportlist_release_event (){
	if [[ $BUTTON = 2 && "$EXPORTLIST" ]]; then
		grep -v "$EXPORTLIST" $WORKDIR/exportlist > $WORKDIR/tmp_exportlist
		mv -f $WORKDIR/tmp_exportlist $WORKDIR/exportlist
	fi
}

set_albumart (){
	TITLE="- $(gettext 'Define albumart sourcefile')"
	CHOOSER_DIR="$STORAGE_DIR/albumart/"
	. $APPDIR/box_chooser 0
	if [ "$EXIT" = "OK" ]; then
		if [ -f "$CHOOSER" ] && [ "`grep -iE "\.jpg$|\.png$|\.gif$" <<< "$CHOOSER"`" ]; then
			rm $WORKDIR/tmp_export_albumart.jpg
			cp "$CHOOSER" $WORKDIR/tmp_export_albumart.jpg
		else
			TXT1="<b>$(gettext 'No valid image file')</b> (jpg/png/gif)"
			TXT2="$CHOOSER"
			. $APPDIR/box_ok
		fi
	fi
}

export -f info fetch_dir exportlist_release_event set_albumart

$APPDIR/func_svg -heading "$(gettext 'Export tracks')" > /dev/null #we don't use the standard xml code here

S='
<window title="pMusic - '$(gettext 'Export')'" icon-name="pmusic">
<vbox spacing="5" '$SF'>


 <hbox spacing="0" height="80" scrollable="true" '$ST' vscrollbar-policy="2" hscrollbar-policy="2">
  <eventbox name="svgheading">
   <hbox '$ST'>
    <text width-request="20" '$SF'><label>""</label></text>
    <text ypad="40" use-markup="true" '$SF'><label>"<i>'$(gettext 'All files in the left pane will be copied or converted to target directory.')'</i>"</label></text>
    <text '$ST'><label>""</label></text>
    <hbox spacing="0" '$SF'>
     <vbox '$SF'>
      <button relief="2" can-focus="false" '$SF'>
       <input file stock="gtk-dialog-warning"></input>
       <action>'$APPDIR'/func -about -disclaimer > /dev/null &</action>
      </button>
      <text '$ST'><label>""</label></text>
     </vbox>
     <vbox>
      <button relief="2" can-focus="false" '$SF'>
       <input file stock="gtk-help"></input>
       <action>info > /dev/null &</action>
      </button>
      <text '$ST'><label>""</label></text>
     </vbox>
    </hbox>
   </hbox>
  </eventbox>
 </hbox>

 <vbox spacing="5" '$ST'>
  <hbox '$ST'>
   <vbox '$ST'>
    <vbox spacing="0" '$ST'>
     <eventbox name="alt_list_header" '$SF'>
      <hbox spacing="0" '$ST'>
       <button relief="2" name="alt_list_header" width-request="50" '$ST'>
        <input file stock="gtk-open"></input>
        <action>fetch_dir</action>
        <action>refresh:EXPORTLIST</action>
       </button>
       <button relief="2" name="alt_list_header" tooltip-text="'$(gettext 'Fetch tracks from Playqueue')'" '$SF'>
        <input file stock="gtk-import"></input>
        <label>" '$(gettext 'PlayQueue')' "</label>
        <action>cut -d"|" -f4 '$WORKDIR'/playlist >> '$WORKDIR'/exportlist</action>
        <action>refresh:EXPORTLIST</action>
       </button>
       <button relief="2" name="alt_list_header" tooltip-text="'$(gettext 'Fetch tracks from Sourcelist')'" '$SF'>
        <input file stock="gtk-import"></input>
        <label>" '$(gettext 'Sourcelist')' "</label>
        <action>cut -d"|" -f14 '$WORKDIR'/sourcelist | grep -E "'$AUDIO_FORMATS'" >> '$WORKDIR'/exportlist</action>
        <action>refresh:EXPORTLIST</action>
       </button>
       <button relief="2" name="alt_list_header" width-request="50" tooltip-markup="'$(gettext 'Clear list.
To remove <b>one</b> item from list, use mouse middle-click.')'" '$ST'>
        <input file stock="gtk-pmusic_clear"></input>
        <action>echo -n > '$WORKDIR'/exportlist</action>
        <action>refresh:EXPORTLIST</action>
       </button>
      </hbox>
     </eventbox>
     <hbox '$ST'>
      <tree stock-id="gtk-audio" file-monitor="true" auto-refresh="true" headers-visible="false" hover-selection="true" '$ST'>
       <variable>EXPORTLIST</variable>
       <input file>'$WORKDIR'/exportlist</input>
       <output file>'$WORKDIR'/exportlist</output>
       <action signal="button-release-event">exportlist_release_event</action>
       <action signal="button-release-event">refresh:EXPORTLIST</action>
      </tree>
     </hbox>
    </vbox>
   </vbox>

   <hbox '$ST'>
    <vbox '$ST'>
     <vbox '$ST'>
      <vbox>
       <hbox>
        <text><label>'$(gettext 'Target directory')'</label></text>
        <entry accept="directory">
         <variable>COPYDIR</variable>
         <input>echo '$COPYDIR'</input>
        </entry>
        <button>
         <input file stock="gtk-open"></input>
         <action type="fileselect">COPYDIR</action>
        </button>
       </hbox>

       <checkbox draw-indicator="false" tooltip-text="'$(gettext 'Converting is required for internet streams, - like tracks from jamendo.com or similar.')''
        type streamripper > /dev/null 2>&1 && S=$S"

$(gettext 'NOTE! Radio-streams can not be converted. They will be recorded in their original format (mp3/ogg). Converting has to be done in a second stage.')"
        S=$S'">
        <label>'$(gettext 'Convert exported tracks')'</label>
        <variable>CHK_OUTPUT_STREAM</variable>
        <default>'$CHK_OUTPUT_STREAM'</default>
        <action>if true show:VBOX_CONVERT2</action>
        <action>if false hide:VBOX_CONVERT2</action>
       </checkbox>

       <vbox visible="'$CHK_OUTPUT_STREAM; S=$S'" spacing="0" '$ST'>
        <notebook labels="'$(gettext 'Quality')'|'$(gettext 'Sound processing')'" '$ST'>
         <vbox '$ST'>
          <hbox spacing="0" '$ST'>
           <vbox spacing="0" '$ST'>
            <tree column-visible="true|false" exported_column="1" spacing="0" '$ST'>
             <label>'$(gettext 'Preset')'|b</label>
             <variable>EXPORT_PRESET</variable>
             <width>140</width>
             <input file>'$WORKDIR'/export_presets</input>'
             COUNT=1
             for I in format bitrate sample channel; do S=$S'
             <action signal="button-release-event">echo "$EXPORT_PRESET" | cut -d, -f'$COUNT' > '$WORKDIR'/export_copy_'$I'</action>
             <action signal="button-release-event">refresh:COPY_'${I^^}'</action>'
             COUNT=$(($COUNT+1))
             done
             S=$S'<action signal="button-release-event">echo COPY_FORMAT=$COPY_FORMAT</action>
             <action signal="button-release-event" condition="command_is_false([ $COPY_FORMAT != flac ] && echo false)">enable:VBOX_CONVERT1</action>
             <action signal="button-release-event" condition="command_is_true([ $COPY_FORMAT = flac ] && echo true)">disable:VBOX_CONVERT1</action>
            </tree>
           </vbox>
           <vbox margin="10" '$SF'>
            <hbox '$SF'>
             <text xalign="0" '$SF'><label>"'$(gettext 'Audio format')' "</label></text>
              <hbox width-request="55" '$SF'>
               <entry width-request="55" '$SF'>
                <variable>COPY_FORMAT</variable>
                <input file>'$WORKDIR'/export_copy_format</input>
               </entry>
              </hbox>
              <hbox width-request="30">
               <menubar width-request="30"><menu>'
                for I in 3gp ac3 aiff amr asf au flac mp2 mp3 mp4 oga ogg opus raw rm voc wav webm; do S=$S'
                <menuitem>
                 <label>'$I'</label>
                 <action>echo '$I' > '$WORKDIR'/export_copy_format</action>
                 <action>refresh:COPY_FORMAT</action>
                 <action>'; [ $I = flac ] && S=$S'disable' || S=$S'enable'; S=$S':VBOX_CONVERT1</action>
                </menuitem>'
                done
                S=$S'<label>..</label>
               </menu></menubar>
              </hbox>
             </hbox>
             <text height-request="8"><label>""</label></text>
             <vbox>
              <hbox>
               <text xalign="0" height-request="18" '$SF'><label>"'$(gettext 'Bitrate (bits/sec)')' "</label></text>
               <entry width-request="90" height-request="18" '$SF'>
                <variable>COPY_BITRATE</variable>
                <input file>'$WORKDIR'/export_copy_bitrate</input>
               </entry>
              </hbox>
              <hbox>
               <text xalign="0" height-request="18" '$SF'><label>"'$(gettext 'Samplerate (hz)')' "</label></text>
               <entry width-request="90" height-request="18" '$SF'>
                <variable>COPY_SAMPLE</variable>
                <input file>'$WORKDIR'/export_copy_sample</input>
               </entry>
              </hbox>
              <hbox>
               <text xalign="0" height-request="18" '$SF'><label>"'$(gettext 'Channels')' "</label></text>
               <entry width-request="90" height-request="18" '$SF'>
                <variable>COPY_CHANNEL</variable>
                <input file>'$WORKDIR'/export_copy_channel</input>
               </entry>
              </hbox>
              <variable>VBOX_CONVERT1</variable>
             </vbox>
            </vbox>
           </hbox>
          </vbox>
          <vbox margin="10" '$ST'>
           <checkbox>
            <variable>BYPASS_FILTERS</variable>
            <default>true</default>
            <label>'$(gettext 'Bypass (keep original sound)')'</label>
            <action>if true disable:VBOX_SOUND_INPUT</action>
            <action>if false enable:VBOX_SOUND_INPUT</action>
           </checkbox>
           <vbox>
           '$GUI_SOUND_INPUT'
           <variable>VBOX_SOUND_INPUT</variable>
           <sensitive>false</sensitive>
          </vbox>
         </vbox>
        </notebook>
        <variable>VBOX_CONVERT2</variable>
       </vbox>

       <vbox '$SF'>
        <checkbox draw-indicator="false" tooltip-text="'$(gettext 'Add numbering to keep sort order (not radio)')'">
         <label>'$(gettext 'Add numbering')'</label>
         <variable>ADD_NUMBERING</variable>
         <default>'$ADD_NUMBERING'</default>
         <action>if true show:HBOX_FIRSTNR</action>
         <action>if false hide:HBOX_FIRSTNR</action>
        </checkbox>
        <hbox visible="'$ADD_NUMBERING; S=$S'">
         <text><label>'$(gettext 'First number')'</label></text>
         <hbox width-request="50">
          <entry accept="directory">
           <variable>FIRST_NR</variable>
           <input>echo '$FIRST_NR'</input>
          </entry>
         </hbox>
         <variable>HBOX_FIRSTNR</variable>
        </hbox>

        <checkbox draw-indicator="false">
         <label>'$(gettext 'Name of exported CD tracks')'</label>
         <action>if true show:HBOX_CD</action>
         <action>if false hide:HBOX_CD</action>
        </checkbox>
        <hbox visible="false">
         <text><label>"       '$(gettext 'See help for info')'"</label></text>
         <entry>
          <variable>CD_FILE_NAME</variable>
          <input>echo "'$CD_FILE_NAME'"</input>
         </entry>
         <variable>HBOX_CD</variable>
        </hbox>

        <checkbox draw-indicator="false">
         <label>'$(gettext 'Albumart to target directory')'</label>
         <variable>ADD_ALBUMART</variable>
         <action>if true show:HBOX_ALBUMART</action>
         <action>if false hide:HBOX_ALBUMART</action>
        </checkbox>
        <hbox visible="false">
         <vbox>
          <hbox '$SF'>
           <button '$SF'>
            <variable>SET_ALBUMART</variable>
            <input file stock="gtk-open"></input>
            <label>'$(gettext 'Another image')'</label>
            <action>set_albumart</action>
            <action>refresh:PIC_ALBUMART</action>
           </button>
          </hbox>
          <text '$ST'><label>""</label></text>
         </vbox>
         <pixmap '$SF'>
          <width>100</width>
          <height>100</height>
          <variable>PIC_ALBUMART</variable>
          <input file>'$WORKDIR'/tmp_export_albumart.jpg</input>
         </pixmap>
         <variable>HBOX_ALBUMART</variable>
        </hbox>

        <checkbox draw-indicator="false" tooltip-markup="'$(gettext 'Filename will be based on the meta information recieved together with the downloaded stream.')'
<b>'$(gettext 'A more comprehensive grabber is found in the Tools menu.')'</b>">
         <label>'$(gettext 'Split radio-stream into tracks')'</label>'
         if ! type streamripper > /dev/null 2>&1; then S=$S'<sensitive>false</sensitive><default>false</default>'; fi
         S=$S'
         <default>'$SPLIT_RADIOSTREAM'</default>
         <variable>SPLIT_RADIOSTREAM</variable>
        </checkbox>
 
       </vbox>
      </vbox>
     </vbox>
    </vbox>
   </hbox>
  </hbox>
  
  <hbox '$SF'>
   <timer visible="false"><action>refresh:EXPORT_STATUSBAR</action></timer>
   <statusbar has-resize-grip="false" file-monitor="true" auto-refresh="true" '$ST'>
    <variable>EXPORT_STATUSBAR</variable>
    <input>echo " $(cat '$WORKDIR'/exportlist | wc -l) '$(gettext 'files added to list')'"</input>
   </statusbar>
   <button cancel></button>
   <button name="highlight" can-default="true" has-default="true" use-stock="true">
    <input file stock="gtk-save-as"></input>
    <label>'$(gettext 'Export')'</label>
    <action type="exit">ok</action>
   </button>
   <statusbar width-request="22"></statusbar>
  </hbox>
 </vbox>
</vbox>
 <action signal="show" condition="command_is_true([[ $(grep -F flac '$WORKDIR'/export_copy_format) ]] && echo true)">disable:VBOX_CONVERT1</action>
</window>'

echo "$S" > $WORKDIR/xml_pmusic_export
[ $TOOLTIPS = false ] && sed -i 's%tooltip-%tooltipXXX%g' $WORKDIR/xml_pmusic_export #deactivate tooltips
. $APPDIR/func -reset_gtk
I=$IFS; IFS=""
for STATEMENTS in $($GTKDIALOG -f $WORKDIR/xml_pmusic_export); do
	eval $STATEMENTS
done
IFS=$I

if [ "$EXIT" = "ok" ]; then
	#msg: no files defined
	if [ ! -s $WORKDIR/exportlist ]; then
		TXT1="$(gettext 'No files is defined. Nothing to do.')"
		. $APPDIR/box_ok
		exit
	fi

	#write rc
	echo "export COPYDIR=\"$COPYDIR\"
export SPLIT_RADIOSTREAM=$SPLIT_RADIOSTREAM
export ADD_NUMBERING=$ADD_NUMBERING
export FIRST_NR=$FIRST_NR

export CHK_OUTPUT_STREAM=$CHK_OUTPUT_STREAM
export COPY_BITRATE=$COPY_BITRATE
export COPY_FORMAT=$COPY_FORMAT
export COPY_SAMPLE=$COPY_SAMPLE
export COPY_CHANNEL=$COPY_CHANNEL

export CD_FILE_NAME=\"$CD_FILE_NAME\"" > $WORKDIR/rcexport

	#splash
	echo 0 > $WORKDIR/splash_copy
	export pmusic_copy_splash='
	<window title="pMusic" icon-name="pmusic" allow-grow="false">
	<vbox>
	 <frame>
	  <text><label>""</label></text>
	  '"<text use-markup=\"true\"><label>\"<b><span color='"'gray30'"' size='"'x-large'"'>pMusic  </span><span color='"'gray30'"' size='"'large'"'>$VERSION</span></b>\"</label></text>
	  <text><label>$(gettext 'Export')</label></text>
	 </frame>
	 <progressbar>
	  <input>"'while [ "$I" != "100" ]; do I=`cat '$WORKDIR'/splash_copy`; TXT=`cat '$WORKDIR'/splash_copy_text`; echo $I; echo "$TXT"; sleep 1; done</input>
	  <action type="exit">Ready</action>
	 </progressbar>
	 <hbox>
	  <button cancel>
	   <action>killall cdda2wav; killall ffmpeg; killall streamripper</action>
	   <action>eject</action>
	   <action>echo 100 > '$WORKDIR'/splash_copy</action>
	   <action>exit:EXIT</action>
	  </button> 
	 </hbox>
	</vbox></window>'
	$GTKDIALOG -p pmusic_copy_splash --center &

	COPYDIR="${COPYDIR}/"; COPYDIR=${COPYDIR//\/\//\/} #be sure to end with one /
	#copy albumart
	if [ $ADD_ALBUMART = true ]; then
		cp $WORKDIR/tmp_export_albumart.jpg "${COPYDIR}folder.jpg"
		cp $WORKDIR/tmp_export_albumart.jpg "${COPYDIR}cover.jpg"
	fi
	#---
	TOTAL=$(grep -c ^ $WORKDIR/exportlist) #for progress in splash-window
	COUNT=0
	while read I; do
		FILE=${I//\`/\\\`}; FILE=${FILE//\"/\\\"}
		NAME=${FILE##*/}; NAME=${NAME%.*};
		[ $ADD_NUMBERING = true ] && ADD_NR="$(printf "%04d" $(($COUNT+$FIRST_NR)))_" #define numbering
		#update progress in spash-window
		COUNT=$(($COUNT+1))
		echo $(($COUNT*100/($TOTAL+1))) > $WORKDIR/splash_copy
		echo "${NAME} ( $COUNT / $TOTAL )" > $WORKDIR/splash_copy_text

		#filters
		if [ "$BYPASS_FILTERS" = "false" ]; then
			if [ $NORMALIZE = true ]; then
				#find possible gain increase
				FF_OUT=$(ffmpeg -i "$FILE" -af volumedetect -nostdin -f null -) #ffmpeg breaks bash loop by always setting stdin. -nostdin avoids this
				TMP=$(grep -F max_volume <<< ${FF_OUT} | cut -d: -f2 | cut -d' ' -f2 | cut -d'-' -f2)
				[ "$TMP" ] && echo "volume=${TMP}dB" > $WORKDIR/ffmpeg_filter_NORMALIZE
			fi
			FILTER="`echo "-af $(<$WORKDIR/ffmpeg_filter_NORMALIZE)$(<$WORKDIR/ffmpeg_filter_EQ)$(<$WORKDIR/ffmpeg_filter_basic)$(<$WORKDIR/ffmpeg_filter)" | sed 's/,"$/"/'`" #_EQ and _basic is used by known plugins. The last is open for external input.
			[ "$FILTER" = '-af ""' ] && FILTER=""
		fi
		#---
		if [ "$CHK_OUTPUT_STREAM" = "true" ]; then
			[ "$COPY_FORMAT" ]	&& OUTPUT_STREAM="-f $COPY_FORMAT "
			[ "$COPY_CHANNEL" ]	&& OUTPUT_STREAM="$OUTPUT_STREAM -ac $COPY_CHANNEL"
			[ "$COPY_SAMPLE" ]	&& OUTPUT_STREAM="$OUTPUT_STREAM -ar $COPY_SAMPLE"
			[ "$COPY_BITRATE" ]	&& OUTPUT_STREAM="$OUTPUT_STREAM -ab $COPY_BITRATE"
		fi
		#CD-audio
		if [[ ${I} = *cdda:* ]]; then
			pmusic -s stop
			#define tags which we stored when reading CD
			IFS=':' read COL1 NR LENGTH NAME <<< "$I"
			IFS='|' read COL1 ARTIST TITLE ALBUM COL5 YEAR GENRE TMP <<< "$(grep "|${NR}|" $WORKDIR/cddb)" #YEAR/GENRE is only avaliable if using cddb_query
			[ ! "$TITLE" ] && TITLE="CD-track ${NR}" #not all CDs are available in the cddb
			NAME="${ADD_NR}`echo "$CD_FILE_NAME" | sed -e "s/%a/$ARTIST/" -e "s/%t/$TITLE/" -e "s/%A/$ALBUM/" -e "s/%n/$NR/"`"
			[ ! "$NAME" ] && NAME="CD-track_${NR}" #if no metatags exists
			#build exec
			[ "$CHK_OUTPUT_STREAM" = "false" ] && COPY_FORMAT=wav #no formating is chosen, but we need one for ffmpeg
			echo "cdda2wav dev=$CD_DEVICE -t $NR -Owav - | ffmpeg -i pipe:0 $FILTER $OUTPUT_STREAM -id3v2_version 3 -metadata artist=\"${ARTIST//\"/\\\"}\" -metadata title=\"${TITLE//\"/\\\"}\" -metadata album=\"${ALBUM//\"/\\\"}\" -metadata year=\"$YEAR\" -metadata track=\"$NR\" -metadata genre=\"$GENRE\" -y \"$COPYDIR$NAME.$COPY_FORMAT\"" > $WORKDIR/exec_copy
			chmod 722 $WORKDIR/exec_copy
			$WORKDIR/exec_copy
			continue
		fi
		#start streamripper if available
		if type streamripper > /dev/null 2>&1 && [[ ${FILE} = *http:* && "`grep -F "$FILE" $WORKDIR/playlist | grep -F '00:00'`" ]]; then
			#test if it is really a radio stream and not a song with a defined length.
			FF_OUT="$(ffmpeg -i "$FILE")"
			if [[ ! "${FF_OUT}" || ${FF_OUT} = *'Duration: N/A,'* ]]; then
				if [ $SPLIT_RADIOSTREAM = true ]; then #split tracks 
					streamripper "$FILE" -d "$COPYDIR" -s -o never -k 1 -u WinampMPEG/5.0
				else
					streamripper "$FILE" -d "$COPYDIR" -A -a -u WinampMPEG/5.0
				fi
				continue
			fi
		fi
		#use ffmpeg
		[ $ADD_NUMBERING != true ] && TARGET=${FILE##*/} || TARGET="${ADD_NR}${FILE##*/}"
		NAME=${TARGET%.*} #remove extension
		#check if already exist
		if [ -f "$COPYDIR""$NAME.$COPY_FORMAT" ]; then
			IMG=dialog-warning; FRAME="$(gettext 'Warning')"; TXT1="<b>$FILE $(gettext 'already exists')</b>"; TXT2="$(gettext 'Do you want to overwrite existing file?')"
			. $APPDIR/box_yesno
			[ $EXIT != yes ] && continue
		fi
		#convert output
		if [ "$CHK_OUTPUT_STREAM" = "true" ]; then
			case $FILE in
				*.kar|*.mid)
					timidity -OF -o $WORKDIR/tmp_midi_export.flac "$FILE"
					FILE=$WORKDIR/tmp_midi_export.flac
					;;
				*youtube.com*)
					. $APPDIR/func -get_youtube_url "$FILE"
					FILE=$URL
					NAME="$ARTIST - $TITLE"
					;;
				*jamendo.com*)
					NAME="`grep -F "$FILE" $WORKDIR/playlist | cut -d'|' -f3`"
					;;
			esac
			echo "ffmpeg -i \"$FILE\" -nostdin $FILTER $OUTPUT_STREAM -y \"$COPYDIR$NAME.$COPY_FORMAT\"" > $WORKDIR/exec_copy
			chmod 722 $WORKDIR/exec_copy
			$WORKDIR/exec_copy
			rm $WORKDIR/tmp_midi_export.flac 2> /dev/null #midi - remove temp file
		else
			cp -f "$FILE" "$COPYDIR$TARGET"
		fi
	done < $WORKDIR/exportlist
	echo 100 > $WORKDIR/splash_copy #reset progress bar
fi
echo -n > $WORKDIR/exportlist

